package cn.edu.scau.dropship.ServiceImpl;

import cn.edu.scau.dropship.entity.User;
import cn.edu.scau.dropship.entity.UserExample;
import cn.edu.scau.dropship.mapper.UserMapper;
import cn.edu.scau.dropship.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public long countByExample(UserExample example) {
        return userMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(UserExample example) {
        return userMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public List<User> selectByExample(UserExample example) {
        return userMapper.selectByExample(example);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(User record, UserExample example) {
        return userMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(User record, UserExample example) {
        return userMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public User selectByName(String name) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andNameEqualTo(name);
        List<User> list=userMapper.selectByExample(userExample);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<User> selectAll() {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andIdIsNotNull();
        List<User> list=userMapper.selectByExample(userExample);
        return list;
    }

    @Override
    public User checkLogin(String name, String password) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andNameEqualTo(name).andPasswordEqualTo(password);
        List<User> list=userMapper.selectByExample(userExample);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public User checkName(String name) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andNameEqualTo(name);
        List<User> list=userMapper.selectByExample(userExample);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<User> selectByType(String type) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andNameEqualTo(type);
        List<User> list=userMapper.selectByExample(userExample);
        return list;
    }
}
